package com.example.demo1.service.impl.service.impl;

import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.entity.BaFunction;
import com.example.demo1.entity.BaMenu;
import com.example.demo1.entity.BaRoleFunction;
import com.example.demo1.mapper.BaFunctionMapper;
import com.example.demo1.mapper.BaMenuMapper;
import com.example.demo1.mapper.BaRoleFunctionMapper;
import com.example.demo1.service.impl.service.FunctionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FunctionServiceImpl implements FunctionService {

    private Logger logger = LoggerFactory.getLogger(FunctionServiceImpl.class);

    @Autowired
    private BaFunctionMapper functionMapper;

    @Autowired
    private BaMenuMapper menuMapper;

    @Autowired
    private BaRoleFunctionMapper roleFunctionMapper;
 
    @Override
    public ServerResponse addFunction(BaFunction function) {
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.addFunction方法调用开始");

        function.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int result = functionMapper.insert(function);
        if(result != 1){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.addFunction方法新增功能失败");
            return ServerResponse.createByErrorMessage("新增功能失败");
        }
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.addFunction方法成功");
        return ServerResponse.createBySuccess("新增功能成功");
    }

    @Override
    public ServerResponse validateFunctionName(String functionName) {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionName方法调用开始");

        if(StringUtils.isEmpty(functionName)){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionName方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        BaFunction function = functionMapper.validateFunctionName(functionName);
        if(function != null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionName方法菜单名称已存在");
            return ServerResponse.createByErrorMessage("功能名称已存在");
        }
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionName方法成功");
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteFunction(Integer functionId) {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法调用开始");

        if(functionId == null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        BaFunction function = functionMapper.selectByPrimaryKey(functionId);
        if(function == null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法欲删除功能不存");
            return ServerResponse.createByErrorMessage("欲删除功能不存在");
        }
        List<BaRoleFunction> functionUsedByRoles = roleFunctionMapper.functionUsedByRoles(functionId);
        if(!CollectionUtils.isEmpty(functionUsedByRoles)){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法删除已被角色绑定的功能报错");
            return ServerResponse.createByErrorMessage("该功能已被现有角色绑定，不能删除");
        }
        List<BaMenu> functionUsedByMenus = menuMapper.functionUsedByMenus(function.getFunctionRightCode());
        if(!CollectionUtils.isEmpty(functionUsedByMenus)){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法删除已被菜单绑定的功能报错");
            return ServerResponse.createByErrorMessage("该功能已被现有菜单绑定，不能删除");
        }
        int result = functionMapper.deleteByPrimaryKey(functionId);
        if(result != 1){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法删除功能失败");
            return ServerResponse.createByErrorMessage("删除功能失败");
        }
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.deleteFunction方法成功");
        return ServerResponse.createBySuccess("删除功能成功");
    }

    @Override
    public ServerResponse updateFunction(BaFunction function) {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.updateFunction方法调用开始");

        if(function.getFunctionId() == null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.updateFunction方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        function.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int result = functionMapper.updateByPrimaryKeySelective(function);
        if(result != 1){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.updateFunction方法修改功能失败");
            return ServerResponse.createByErrorMessage("修改功能失败");
        }
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.updateFunction方法成功");
        return ServerResponse.createBySuccess("修改功能成功");
    }

    @Override
    public ServerResponse validateFunctionNameExSelf(String functionName, Integer functionId) {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionNameExSelf方法调用开始");

        if(StringUtils.isEmpty(functionName)){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionNameExSelf方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        BaFunction function = functionMapper.validateFunctionNameExSelf(functionName, functionId);
        if(function != null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionNameExSelf方法功能名称已存在");
            return ServerResponse.createByErrorMessage("功能名称已存在");
        }
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.validateFunctionNameExSelf方法成功");
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse<List<BaFunction>> getAllFunctions() {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.getAllFunctions方法调用开始");

        List<BaFunction> allFunctions = functionMapper.selectFunctionsByState();
        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.getAllFunctions方法成功");
        return ServerResponse.createBySuccess(allFunctions);
    }

    @Override
    public ServerResponse getAllFunctionsByParamLimit(String searchStr, Integer pageNum, Integer pageSize) {

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.getAllFunctionsByParamLimit方法调用开始");

        //1.参数校验
        if(searchStr == null){
            logger.info("时间：" + new Date() + "=================FunctionServiceImpl.getAllFunctionsByParamLimit方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        //2.分页查询权限信息
        PageHelper.startPage(pageNum, pageSize);
        List<BaFunction> functions = functionMapper.getAllFunctionsByParamLimit(searchStr);
        PageInfo pageInfo = new PageInfo(functions);

        logger.info("时间：" + new Date() + "=================FunctionServiceImpl.getAllFunctionsByParamLimit方法执行成功");
        return ServerResponse.createBySuccess(pageInfo);
    }

}
